package open.tech.dawn.rest.module.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import open.tech.dawn.core.util.XssUtil;
import open.tech.dawn.rest.common.annot.Menu;
import open.tech.dawn.rest.common.page.PageFactory;
import open.tech.dawn.rest.common.page.PageInfo;
import open.tech.dawn.rest.module.system.entity.SysConfigDto;
import open.tech.dawn.rest.module.system.model.SysConfig;
import open.tech.dawn.rest.module.system.query.ConfigQuery;
import open.tech.dawn.rest.module.system.service.ISysConfigService;
import open.tech.dawn.web.core.aop.Permission;
import open.tech.dawn.web.core.controller.BaseController;
import open.tech.dawn.web.core.support.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("system/config")
@Menu(name = "系统配置", code = "sys_config", icon = "fa fa-cubes", url = "/views/system/config/config", order = 5, page = true, parentCode = "system")
public class SysConfigController extends BaseController {

    @Resource
    private ISysConfigService sysConfigService;

    /**
     * 配置详细
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "info", method = {RequestMethod.GET})
    @Permission
    @Menu(name = "配置详细", code = "sys_config_info", parentCode = "sys_config")
    public Object info(String id) {
        return Result.createSuccess(sysConfigService.getById(id));
    }

    /**
     * 配置列表
     *
     * @param query
     * @return
     */
    @RequestMapping(value = "list", method = {RequestMethod.POST})
    @Permission
    @Menu(name = "配置列表", code = "sys_config_list", parentCode = "sys_config")
    public Object list(@RequestBody ConfigQuery query) {
        Page<SysConfig> page = new PageFactory<SysConfig>().create(query);
        page = sysConfigService.queryPage(page, query);
        return new PageInfo<>(page);
    }

    /**
     * 更新配置
     *
     * @return
     */
    @Permission
    @RequestMapping(value = "delete", method = {RequestMethod.GET})
    @Menu(name = "删除配置", code = "sys_config_delete", parentCode = "sys_config")
    public Object delete(String id) {
        sysConfigService.removeById(id);
        return Result.createSuccess();
    }


    /**
     * 更新配置
     *
     * @return
     */
    @Permission
    @RequestMapping(value = "update", method = {RequestMethod.POST})
    @Menu(name = "更新配置", code = "sys_config_update", parentCode = "sys_config")
    public Object update(@RequestBody SysConfigDto sysConfig) {
        sysConfig.setName(XssUtil.reduceXSS(sysConfig.getName()));
        sysConfig.setValue(XssUtil.reduceXSS(sysConfig.getValue()));
        sysConfigService.update(sysConfig);
        return Result.createSuccess();
    }

    /**
     * 新增配置
     *
     * @param sysConfig
     * @return
     */
    @Permission
    @RequestMapping(value = "add", method = {RequestMethod.POST})
    @Menu(name = "新增配置", code = "sys_config_add", parentCode = "sys_config")
    public Object add(@RequestBody SysConfigDto sysConfig) {
        sysConfig.setName(XssUtil.reduceXSS(sysConfig.getName()));
        sysConfig.setValue(XssUtil.reduceXSS(sysConfig.getValue()));
        sysConfigService.add(sysConfig);
        return Result.createSuccess();
    }

}
